#include <func.h>

struct student {
	int id;
	char name[25];
};

void print_ids(const char* prefix) {
	printf("%s: pid = %d, tid = 0x%lx\n",
		   prefix, getpid(), pthread_self());
}

void* start_routine(void* arg) {
	struct student* p = (struct student*) arg;
	printf("new thread: BEGINE\n");
	print_ids("new thread");
	printf("new thread: {%d, %s}\n", p->id, p->name); // 新线程访问main线程栈中的数据
	printf("new thread: END\n");

	return NULL;
}

int main(int argc, char* argv[])
{
	// 1. 创建新线程
	int err;
	pthread_t tid;
	struct student s1 = {1, "xixi"}; // main 函数的栈中
	err = pthread_create(&tid, NULL, start_routine, (void*)&s1);
	// err = pthread_create(&tid, NULL, start_routine, (void*)1);
	// err = pthread_create(&tid, NULL, start_routine, NULL);
	if (err) {
		error(1, err, "pthread_create");
	}

	printf("main thread: new thread id = 0x%lx\n", tid);

	sleep(1);

	print_ids("main thread");
    return 0; // exit(0), 进程退出，进程中所有线程都终止。
}
